//
//  BJMineViewController.m
//  BowJoy
//
//  Created by  on 2024/5/13.
//

#import "BJMineViewController.h"
#import "BJEditViewController.h"
#import "BJAboutViewController.h"
#import "BJSettingViewController.h"
#import "BJFollowListViewController.h"
#import "BJBlockListViewController.h"
#import "BJCoinStoreViewController.h"
#import "BJPictureFrameViewController.h"
#import "BJMineArticleViewController.h"

@interface BJMineViewController ()
@property (weak, nonatomic) IBOutlet UIView *bgGroundView;

@property (weak, nonatomic) IBOutlet UIView *coinView;
@property (weak, nonatomic) IBOutlet UIImageView *settngImage;

@property (nonatomic) BJGetUserInfoModelData *userInfoData;


@property (weak, nonatomic) IBOutlet UILabel *coinsL;
@property (weak, nonatomic) IBOutlet UIImageView *headerFramImage;


@property (weak, nonatomic) IBOutlet UIImageView *headerImage;

@property (weak, nonatomic) IBOutlet UILabel *nikeNameL;

@property (weak, nonatomic) IBOutlet UILabel *idLable;


@property (weak, nonatomic) IBOutlet UILabel *followerL;


@property (weak, nonatomic) IBOutlet UILabel *followingL;

@property (weak, nonatomic) IBOutlet UILabel *blockLable;


@end

@implementation BJMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self bindingEvent];
    [self getOssPolicyData];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applePaySuccess) name:kNotificationAppleSuccessfulPurchase object:nil];////商品购买(支付)成功通知

}
-(void)applePaySuccess{
    [self requestMyCoin];
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //埋点
    NSMutableArray *dataList = [NSMutableArray array];
    NSDictionary *dict = @{
        @"page":@"mine",
        @"duration":@(0),
        @"event":@"pv",
        @"tm":@([[BJNetManager shareNetManager] gs_getCurrentTimeToMilliSecond]),
    };
    [dataList addObject:dict];
    [[BJNetManager shareNetManager]postLogWithParam:dataList];
        

    [self requestMyCoin];
    [self getUserInfo];


}

///获取用户新数据
- (void)getUserInfo{
    NSString *iconPictureFrame =  [[NSUserDefaults standardUserDefaults] objectForKey:KSelectPictureFrameKey];
    if (iconPictureFrame) {
        self.headerFramImage.image = [UIImage imageNamed:iconPictureFrame];
    }

    self.followingL.text = [NSString stringWithFormat:@"%ld",[BJUserManager sharedManager].followingNum];
    self.blockLable.text = [NSString stringWithFormat:@"%ld",[BJUserManager sharedManager].blockNum];

    
    kWeakSelf(weakSelf)
    [[BJNetManager shareNetManager] getGetUserInfoWithUserId:[[BJUserManager sharedManager] getUserId] completionHandler:^(BJGetUserInfoModel * _Nonnull model, NSError * _Nonnull error) {
        if (!error && [model.code intValue] == 0){
            weakSelf.userInfoData = model.data;
            [BJUserManager sharedManager].userInfoData = model.data;
            weakSelf.coinsL.text = [NSString stringWithFormat:@"%@",model.data.availableCoins];
            [weakSelf.headerImage setImageURL:[NSURL URLWithString:model.data.avatarUrl]];
            if(model.data&&model.data.nickname.length>12){
               NSString * nameStr = [model.data.nickname substringToIndex:12];
                weakSelf.nikeNameL.text = [NSString stringWithFormat:@"%@...",nameStr];
            }else{
                weakSelf.nikeNameL.text = model.data.nickname;
            }
            weakSelf.idLable.text = [NSString stringWithFormat:@"ID: %@",model.data.userId];

        }

        
    }];
}
-(void)requestMyCoin{
    kWeakSelf(weakSelf)

    [[BJNetManager shareNetManager]getUserCurrentCoinsWithcompletionHandler:^(BJUserCoinsModel * _Nonnull model, NSError * _Nonnull error) {
        if(!error){
            weakSelf.coinsL.text = [NSString stringWithFormat:@"%ld",(long)model.data.availableCoins];
        }
    }];
}


//获取相册拍照数据
- (void)getOssPolicyData{
    [[BJNetManager shareNetManager] getOssPolicyWithcompletionHandler:^(BJOssPolicModel * _Nonnull model, NSError * _Nonnull error) {
            if(!error && model.code ==0){
                [BJConfigManager sharedManager].ossPolicModel = model.data;
            }
    }];

}



- (void)bindingEvent {
    __weak typeof(self) weakSelf = self;

    self.coinView.onClick(^{
        BJCoinStoreViewController *coinStoreView = [[BJCoinStoreViewController alloc]init];
        [weakSelf.navigationController pushViewController:coinStoreView animated:YES];
    });
    
    self.settngImage.onClick(^{
        BJSettingViewController *setTingView = [[BJSettingViewController alloc]init];
        [weakSelf.navigationController pushViewController:setTingView animated:YES];
    });
}


- (IBAction)clickEditBtn:(id)sender {
    BJEditViewController *editView = [[BJEditViewController alloc]init];
    __weak typeof(self) weakSelf = self;
    [editView setUpdateUserInfo:^{
        [weakSelf getUserInfo];
    }];
    [self.navigationController pushViewController:editView animated:YES];
}


- (IBAction)clickCopyBtn:(id)sender {
    UIPasteboard *copyID = [UIPasteboard generalPasteboard];
    copyID.string = self.userInfoData.userId;
    [ASHUD showWithText:@"Copy Success" afterDelay:1.0];

}

- (IBAction)clickListBtn:(UIButton *)sender {
    if (sender.tag == 100) {
        BJMineArticleViewController *view = [[BJMineArticleViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }else if (sender.tag == 101){
        BJFollowListViewController *followListView = [[BJFollowListViewController alloc]init];
        [self.navigationController pushViewController:followListView animated:YES];
    }else if (sender.tag == 102){
        BJBlockListViewController *blockListView = [[BJBlockListViewController alloc]init];
        [self.navigationController pushViewController:blockListView animated:YES];
    }else if (sender.tag == 103){
        __weak typeof(self) weakSelf = self;

        BJPictureFrameViewController *pictureFrameView = [[BJPictureFrameViewController alloc]init];
        pictureFrameView.updateUserHeaderImage = ^{
            NSString *iconPictureFrame =  [[NSUserDefaults standardUserDefaults] objectForKey:KSelectPictureFrameKey];
            if (iconPictureFrame) {
                weakSelf.headerFramImage.image = [UIImage imageNamed:iconPictureFrame];
            }
        };
        [self.navigationController pushViewController:pictureFrameView animated:YES];
    }else if (sender.tag == 104){
        BJAboutViewController *aboutView = [[BJAboutViewController alloc]init];
        [self.navigationController pushViewController:aboutView animated:YES];
    }

    
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
